﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MODULE_KETOAN
{
    public partial class frm_rpt_bangkethanhtoan : Form
    {
       // DataTable dt;
        ketoanDataContext context = cls_ketoan.getcontext();
        public frm_rpt_bangkethanhtoan()
        {
            InitializeComponent();
        }

        private void frm_rpt_bangkethanhtoan_Load(object sender, EventArgs e)
        {
           

            this.rdbnhapvathanhtoan.Checked = true;
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            try
            {
                
                string tungay, denngay;
                string d, m, y;

                if (this.dtptungay.Value.Day >= 10)
                {
                    d = this.dtptungay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtptungay.Value.Day.ToString();
                }

                if (this.dtptungay.Value.Month >= 10)
                {
                    m = this.dtptungay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtptungay.Value.Month.ToString();
                }
                y = this.dtptungay.Value.Year.ToString();
                tungay = d + "/" + m + "/" + y;
                if (this.dtpdenngay.Value.Day >= 10)
                {
                    d = this.dtpdenngay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpdenngay.Value.Day.ToString();
                }

                if (this.dtpdenngay.Value.Month >= 10)
                {
                    m = this.dtpdenngay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpdenngay.Value.Month.ToString();
                }
                y = this.dtpdenngay.Value.Year.ToString();
                denngay = d + "/" + m + "/" + y;
                if (rdbnhapvathanhtoan.Checked == true)
                {



                    //IEnumerable<KT_rpt_bangkethanhtoantiennhapnguyenlieuResult> lst = context.KT_rpt_bangkethanhtoantiennhapnguyenlieu(tungay, denngay).AsEnumerable();
                    string connectstring = context.Connection.ConnectionString;
                    SqlConnection connection = new SqlConnection(connectstring);
                    SqlCommand command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "KT_rpt_bangkethanhtoantiennhapnguyenlieu'" + tungay + "','" + denngay + "'";

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable tb = new DataTable();
                    adapter.Fill(tb);



                    MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();
                    MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                    //DataTable dt = cls.IEnumerabletoDataTable(lst);
                    Cursor.Current = Cursors.WaitCursor;
                    
                    objReport.Load_Report(Application.StartupPath + "\\reports\\bangkethanhtoantiennhapnguyenlieu.rpt", tb);
                     
                    //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                    objReport.Formulas_Report("tungay", "'"+tungay+"'");
                    objReport.Formulas_Report("denngay", "'"+denngay+"'");
                    objReport.Open_Report();
                    Cursor.Current = Cursors.Default;
                    //objCtrl = Nothing
                    //objReport = Nothing

                }
                if (rdbthanhtoan.Checked == true)
                {
                    IEnumerable<KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieuResult> lst = context.KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieu().AsEnumerable();

                    string connectstring = context.Connection.ConnectionString;
                    SqlConnection connection = new SqlConnection(connectstring);
                    SqlCommand command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieu";

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable tb = new DataTable();
                    adapter.Fill(tb);
                    MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();


                    Cursor.Current = Cursors.WaitCursor;
                    objReport.Load_Report(Application.StartupPath + "\\reports\\bangkechungtuthanhtoantiennhapnguyenlieu.rpt", lst);
                    //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                    objReport.Open_Report();
                    //objCtrl = Nothing
                    //objReport = Nothing
                    Cursor.Current = Cursors.Default;


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btExcel_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.Filter = "Excel file(*.xls)|*.xls";
           
            
           
            try
            {
                string tungay, denngay;
                string d, m, y;

                if (this.dtptungay.Value.Day >= 10)
                {
                    d = this.dtptungay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtptungay.Value.Day.ToString();
                }

                if (this.dtptungay.Value.Month >= 10)
                {
                    m = this.dtptungay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtptungay.Value.Month.ToString();
                }
                y = this.dtptungay.Value.Year.ToString();
                tungay = d + "/" + m + "/" + y;
                if (this.dtpdenngay.Value.Day >= 10)
                {
                    d = this.dtpdenngay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpdenngay.Value.Day.ToString();
                }

                if (this.dtpdenngay.Value.Month >= 10)
                {
                    m = this.dtpdenngay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpdenngay.Value.Month.ToString();
                }
                y = this.dtpdenngay.Value.Year.ToString();
                denngay = d + "/" + m + "/" + y;
                if (rdbnhapvathanhtoan.Checked == true)
                {


                    this.saveFileDialog1.FileName = "bangkethanhtoantiennhapnguyenlieu.xls";
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        Cursor.Current = Cursors.WaitCursor;


                        List<KT_rpt_bangkethanhtoantiennhapnguyenlieuResult> lst = context.KT_rpt_bangkethanhtoantiennhapnguyenlieu(tungay, denngay).AsEnumerable().ToList();


                        MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();
                        MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                        DataTable dt = cls.IEnumerabletoDataTable(lst);
                        grdata.Rows.Clear();

                        if (lst != null)
                        {

                            for (int i = 0; i < lst.Count(); i++)
                            {
                                KT_rpt_bangkethanhtoantiennhapnguyenlieuResult ele = lst.ElementAt(i);
                                grdata.Rows.Add();
                                grdata.Rows[i].Cells["so"].Value = ele.so;
                                grdata.Rows[i].Cells["ngay"].Value = ele.ngay;
                                grdata.Rows[i].Cells["tenchuhang"].Value = ele.tenchuhang;
                                grdata.Rows[i].Cells["diachi"].Value = ele.diachichuhang;
                                grdata.Rows[i].Cells["tienhang"].Value = ele.tienhang;
                                grdata.Rows[i].Cells["sophieu"].Value = ele.sophieu;
                                grdata.Rows[i].Cells["sotien"].Value = ele.tienhang;

                            }
                        }


                        String filePath = saveFileDialog1.FileName;

                        ExcelWrite myExcel = new ExcelWrite();
                        List<String> list = myExcel.DGVToExcel(grdata);
                        //String filePath = path + "\\BangKeThanhToan.xls";

                        myExcel.WriteDateToExcel(filePath, list, "A1", "G1", "A2");
                        MessageBox.Show("Export dữ liệu thành công ...", "Thông báo");
                        Cursor.Current = Cursors.Default;
                    }

                }
                if (rdbthanhtoan.Checked == true)
                {
                    
                    this.saveFileDialog1.FileName = "bangkechungtuthanhtoantiennhapnguyenlieu.xls";

                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        List<KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieuResult> lst = context.KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieu().ToList();

                        MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();
                        MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                        DataTable dt = cls.IEnumerabletoDataTable(lst);

                        if (lst != null)
                        {

                            for (int i = 0; i < lst.Count(); i++)
                            {
                                KT_rpt_bangkechungtuthanhtoantiennhapnguyenlieuResult ele = lst.ElementAt(i);
                                grdata1.Rows.Add();
                                grdata1.Rows[i].Cells["so1"].Value = ele.so;
                                grdata1.Rows[i].Cells["ngay1"].Value = ele.ngay;
                                grdata1.Rows[i].Cells["tenchuhang1"].Value = ele.tenchuhang;
                                grdata1.Rows[i].Cells["diachi1"].Value = ele.diachi;
                                grdata1.Rows[i].Cells["tienhang1"].Value = ele.tienhang;
                                grdata1.Rows[i].Cells["VAT"].Value = ele.VAT;
                                grdata1.Rows[i].Cells["thucchi"].Value = ele.tienhang;

                            }
                        }



                        String filePath = saveFileDialog1.FileName;

                        ExcelWrite myExcel = new ExcelWrite();
                        List<String> list = myExcel.DGVToExcel(grdata1);
                        //String filePath = path + "\\BangKeThanhToan.xls";

                        myExcel.WriteDateToExcel(filePath, list, "A1", "G1", "A2");
                        MessageBox.Show("Export dữ liệu thành công ...", "Thông báo");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      
        
    }
}
